<template>
  <div class="goods-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索、添加区域 -->
      <el-row :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="8">
          <!-- change事件：搜索框失去焦点(blur)或用户点击enter键触发，回调为当前的输入值
            不需要调用输入框的clear事件，调用后点击删除按钮会分别执行clear和change事件对应的函数 -->
          <el-input placeholder="请输入内容" clearable v-model="goodsName" @change="serchGoods">
            <!-- 输入框的change事件与搜索按钮的click事件都会触发serchGoods函数 -->
            <el-button slot="append" icon="el-icon-search" @click="serchGoods"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-col :span="4">
          <el-button type="info" @click="gotoAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column label="索引" type="index" width="60"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" max-width="700"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="120"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80"></el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scope">
            <!-- 调用main.js中的全局过滤器dateFormat将时间戳转换成年月日时分秒 -->
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="180">
          <el-button type="primary" round size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" round size="mini" icon="el-icon-delete">删除</el-button>
        </el-table-column>
      </el-table>
      <!-- 商品列表分页菜单 -->
      <!-- vue的事件处理函数默认参数为$event对象，可省略，在methods对象中定义对应事件处理函数时可使用形参重命名$event -->
      <el-pagination
        @size-change="handleSizeChange($event)"
        @current-change="handleCurrentChange"
        :current-page="pnum"
        :page-sizes="[8, 10, 15, 20]"
        :page-size="psize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsList } from '@/api/goodsApi.js'
export default {
  name: 'myGoods',
  data () {
    // 自定义表单校验规则
    const checkCat = (rule, value, cb) => {
      const regCat = /\[(.+)\]/
      if (regCat.test(value)) return cb()
      return cb(new Error('请输入列表形式数据'))
    }
    return {
      goodsName: '',
      goodsList: [],
      pnum: 1,
      psize: 10,
      total: 0,
      // 添加商品表单数据
      addGoodsForm: {},
      // 添加商品表单验证对象
      addGoodsRules: {
        goods_name: [
          { required: true, message: '请输入商品名', trigger: 'blur' }
        ],
        goods_cat: [
          { required: true, message: '请输入商品分类', trigger: 'blur' },
          { validator: checkCat, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 初始化商品列表（获取商品列表）
    async initGoodsList () {
      /**
       * 在传入goodsName参数用于查询，
       * 默认为空：即获取所有商品
      */
      const { data: res } = await getGoodsList(this.goodsName, this.pnum, this.psize)
      if (res.meta.status !== 200) {
        return this.$notify.error({ title: '商品列表获取失败', message: res.meta.msg })
      }
      // 没有return 出去说明获取商品列表成功
      this.goodsList = res.data.goods
      this.total = res.data.total
      console.log(res.data)
    },
    // 分页菜单每页商品条数变化函数
    handleSizeChange (newSize) {
      // console.log(newSize)
      this.psize = newSize
      this.initGoodsList()
    },
    // 分页菜单页数变化函数
    handleCurrentChange (newPage) {
      this.pnum = newPage
      this.initGoodsList()
    },
    // 搜索框清空按钮的事件
    serchClear () {
      // 重新请求完整的商品列表
      this.initGoodsList()
      // console.log(this.goodsName)
    },
    // 搜索商品事件的处理函数
    serchGoods () {
      // 根据当前goodsName中的值发起请求
      this.initGoodsList()
      // console.log(this.goodsName)
    },
    // 跳转至添加商品页面
    gotoAdd () {
      this.$router.push('/goods/add')
    }
  },
  created () {
    this.initGoodsList()
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
</style>
